function successful=runVenCmnd(cmdFile,vensimNm, venDir,finFlNm,logFile,maxTrial)

%% controlling vengine
% Inputs:
% command file to be executed (cmdFile)
% expected duration of command in seconds (durScnds)
% vensim file name (vensimNm)
% vensim exe file and directory (venDir)
% file name to be found in the directory if correctly complete (finFlNm)
% maximum number of trials before quitting

import java.awt.Robot;
import java.awt.event.*;

shortPause=1; % time between different checks for availability of vensim
moderatePause=3; %time for first pause to get vensim to work
longPause=5; % time between different updates to log file to kill vensim
if nargin<6
    maxTrial=5;
end
outSig=1;
numTrial=0;


successful=0;%isfile(finFlNm);
while ~successful && numTrial<maxTrial
    % send command to vensim
    numTrial=numTrial+1;
    if isfile(finFlNm)
        delete(finFlNm,logFile);
    end
    system(['"' venDir vensimNm '" "' cmdFile '" &']);          % This is the main line calling Vensim and passing it the relevant command file; the & gives control back to matlab
    pause(0.05);
    % click to start vengine runs
    
    mouse = Robot;
    
    mouse.mouseMove(0, 0);
    %    mouse.mouseMove(810, 520);
    mouse.mouseMove(1430, 780);
    mouse.mousePress(InputEvent.BUTTON1_MASK);    %//left click press
    pause(0.02);
    mouse.mouseRelease(InputEvent.BUTTON1_MASK);   %//left click release
    venIsAlive=1;
    
    
    % wait for a short duration before checking anything
    % pause(shortPause);
    
    %venIsAlive=checkRunningProcess(vensimNm);
    % check if vensim is active
    %venRecentUpdate=checkLastUpdate(finFlNm,1);
    
    
    cntWh=0;
    
    %if not successful yet, go into the loop
    while ~successful && venIsAlive
        if cntWh==0
            pause(moderatePause);
        else
            pause(shortPause);
        end
        cntWh=cntWh+1;
        
        % click to remove error message if any
        
        %mouse = Robot;
        
        
        if ~isempty(logFile)
            venRecentUpdate=checkLastUpdate(logFile,longPause);
        else
            venRecentUpdate=1;
        end
        
      
        
        % successful=checkLastUpdate(finFlNm,shortPause);
        successful=isfile(finFlNm);
        venIsAlive=checkRunningProcess(vensimNm);
        if venIsAlive && ~venRecentUpdate 
            
            
            commandKill = sprintf('taskkill /F /FI "IMAGENAME eq %s"', vensimNm);
            system(commandKill);
            venIsAlive=0;
            
        end
        %    mouse.mouseMove(0, 0);
        %mouse.mouseMove(1430, 780);
        % mouse.mouseMove(810, 520);
        
        %mouse.mouseMove(750, 520);
        %mouse.mousePress(InputEvent.BUTTON1_MASK);    %//left click press
        %pause(0.02);
        %mouse.mouseRelease(InputEvent.BUTTON1_MASK);   %//left click release
    end
    %
    
    if checkRunningProcess(vensimNm)
        
        commandKill = sprintf('taskkill /F /FI "IMAGENAME eq %s"', vensimNm);
        system(commandKill);
        venIsAlive=0;
    end
    %}
end




end
